import 'package:flutter/material.dart';
import '../../l10n/app_localizations.dart';
import '../controller/webview.dart';
import '../../manger/global_config.dart';

class ProtocolView extends StatefulWidget {
  const ProtocolView({super.key});

  static _ProtocolViewState? of(BuildContext context) {
    final state = context.findAncestorStateOfType<_ProtocolViewState>();
    return state;
  }

  static bool get isSelected {
    return _ProtocolViewState._isSelected;
  }

  static set isSelected(bool value) {
    _ProtocolViewState._isSelected = value;
  }

  @override
  State<ProtocolView> createState() => _ProtocolViewState();
}

class _ProtocolViewState extends State<ProtocolView> {
  static bool _isSelected = false;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 5),
      child: Row(
        children: [
          GestureDetector(
            onTap: () {
              setState(() {
                _isSelected = !_isSelected;
              });
            },
            child: SizedBox(
              width: 24,   // 容器的宽度
              height: 24,  // 容器的高度
              child: Center(  // 保证图片居中
                child: Image.asset(
                  _isSelected ? 'assets/images/login_select.png' : 'assets/images/login_unselect.png',
                  width: 16,  // 图片宽度
                  height: 16, // 图片高度
                  fit: BoxFit.contain,  // 保持图片比例，确保图片不会变形
                ),
              ),
            ),

          ),
          const SizedBox(width: 8),
          Expanded(
            child: GestureDetector(
              onTap: () {
                // 处理协议文本点击事件
              },
              child: RichText(
                text: TextSpan(
                  style: const TextStyle(
                    fontSize: 12,
                    color: Color(0xFF999999),
                  ),
                  children: [
                    TextSpan(text: AppLocalizations.of(context)!.iHaveReadAndAgree),
                    WidgetSpan(
                      alignment: PlaceholderAlignment.middle,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => WebViewPage(
                                title: AppLocalizations.of(context)!.news,
                                url: UrlConfig.payProtocalURL(context),
                              ),
                            ),
                          );
                        },
                        child: Text(
                          AppLocalizations.of(context)!.userAgreement,
                          style: const TextStyle(fontSize: 12,color: Color(0xFF4187FD)),
                        ),
                      ),
                    ),
                    if (!isChineseLocale())
                    TextSpan(text: " and"),
                    WidgetSpan(
                      alignment: PlaceholderAlignment.middle,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => WebViewPage(
                                title: AppLocalizations.of(context)!.news,
                                url: UrlConfig.privacyPolicyURL(context),
                              ),
                            ),
                          );      
                        },
                        child: Text(
                          AppLocalizations.of(context)!.privacyAgreement,
                          style: const TextStyle(fontSize: 12,color: Color(0xFF4187FD)),
                        ),
                      ),
                    ),
                    if (isChineseLocale())
                    WidgetSpan(
                      alignment: PlaceholderAlignment.middle,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => WebViewPage(
                                title: AppLocalizations.of(context)!.news,
                                url: UrlConfig.mianURL(context),
                              ),
                            ),
                          );                          
                        },
                        child: Text(
                          AppLocalizations.of(context)!.disclaimer,
                          style: const TextStyle(fontSize: 12,color: Color(0xFF4187FD)),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}